package zz.demo.springCloud.wechat.config;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import zz.demo.springCloud.wechat.entity.WeChatApp;
import zz.demo.springCloud.wechat.service.WeChatAppService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangz on 2017/11/9.
 */
@Component
public class WxOperations {
    @Autowired
    @Qualifier("wxCpServiceList")
    private List<WxCpServiceAdapter> wxCpServiceList;
    @Autowired
    private WxCpServiceAdapter wxCpServiceAdapter;
    @Autowired
    private WeChatAppService weChatAppService;

    WxError errorRepeat = WxError.newBuilder().setErrorMsg("Repeated application!").setErrorCode(1).build();

    /*
        本对象新增一条service，并检查。一般情况下不调用此方法。
        每次add前都需要检查此次添加的accessPath是否重复。若重复则覆盖。
     */
    public void add(WxCpServiceAdapter wxCpService) throws WxErrorException {
        for(WxCpServiceAdapter wxCpServiceAdapter:wxCpServiceList){
            if(wxCpServiceAdapter.getWeChatApp().getAccessPath().equals(wxCpService)){
                wxCpServiceAdapter = wxCpService;
                checkAccessPath();
                return;
            }
        }
        wxCpServiceList.add(wxCpService);
        checkAccessPath();
    }

    public WxCpServiceAdapter addAndGet(String accessPath) throws WxErrorException {
        WeChatApp weChatApp = weChatAppService.findByAccessPath(accessPath);
        if(weChatApp != null){
            WxCpServiceAdapter wxCpService = wxCpServiceAdapter.build(weChatApp);
            add(wxCpService);
            return wxCpService;
        }
        return null;
    }

    public List<WxCpServiceAdapter> getAll(){
        return this.wxCpServiceList;
    }

    /*
        直接刷新本实例list全体。有不可逆的bug时才使用。
     */
    public void refreshAll() throws WxErrorException {
        List<WeChatApp> weChatAppList = weChatAppService.findAllAndCheck();
        this.wxCpServiceList = new ArrayList<WxCpServiceAdapter>();
        for(WeChatApp weChatApp:weChatAppList){
            this.wxCpServiceList.add(this.wxCpServiceAdapter.build(weChatApp));
        }
    }

    /*
        仅检查对象内accessPath是否重复。
        如果重复，就删除前一条accessPath
     */
    public void checkAccessPath() throws WxErrorException{
        ArrayList<Integer> accessPathList = new ArrayList<Integer>();
        for(int i=0;i<wxCpServiceList.size()-1;i++){
            for(int j=i+1;i<wxCpServiceList.size();j++){
                if(wxCpServiceList.get(i).getWeChatApp().equals(wxCpServiceList.get(j).getWeChatApp().getAccessPath())){
                    wxCpServiceList.remove(i);
                    i--;
                    break;
                }
            }
        }
    }

    public WxCpServiceAdapter findByAccessPath(String accessPath){
        for(WxCpServiceAdapter wxCpServiceAdapter :wxCpServiceList){
            if(wxCpServiceAdapter.getWeChatApp().getAccessPath().equals(accessPath)){
                return wxCpServiceAdapter;
            }
        }
        return null;
    }

    public WxCpServiceAdapter findByAgentId(Integer agentId){
        for(WxCpServiceAdapter wxCpServiceAdapter :wxCpServiceList){
            if(wxCpServiceAdapter.getWxCpConfigStorage().getAgentId().equals(agentId)){
                return wxCpServiceAdapter;
            }
        }
        return null;
    }

    /*
        先查找bean中service对象，若没有则取数据库中查找。
     */
    public WxCpServiceAdapter searchAndFindByAccessPath(String accessPath) throws WxErrorException {
        WxCpServiceAdapter wxCpService = findByAccessPath(accessPath);
        if(wxCpService == null){
            wxCpService = addAndGet(accessPath);
        }
        return wxCpService;
    }

}
